<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_aihire/model')->方法名();
*/
namespace jy_aihire;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function pdf2png($pdf='', $path='',$imgs=array()){
		$ext=strtolower(substr(strrchr($pdf,'.'),1));
		if($ext!='pdf'){
            exi('请上传PDF文件！','error');
        }
		//部分服务器需要安装ghostscript；命令：yum install ghostscript
        if (!extension_loaded('imagick')) {
           exi('请先安装PHP imagemagick扩展！','error');
        }
		$pdf=ROOT_AT.$pdf;
        if (!file_exists($pdf)) {
            exi('请上传PDF文件！','error');
        }
        $im = new \Imagick();
        $im->setResolution(120, 120); //设置分辨率 值越大分辨率越高
        $im->setCompressionQuality(100);
        $im->readImage($pdf);
		$i=0;
        foreach($im as $k => $v) {
			$filename=cfc('files')->get_source_name('image','png');
			$pathimage=$path?:ROOT_AT.$filename;
            $v->setImageFormat('png');
            $fileName2 = $pathimage;
            if ($v->writeImage($fileName2) == true) {
                $images[] = $filename;
				$i++;
            }
        }
        return $images;
    }
	public function daoru($post=array()){
		if(!$post['excel']){
			exi('请先上传CSV文件！','error');
		}
		$localexcel=cfc('files')->local($post['excel']);
		$excel=cfc('tools')->importExcel($localexcel);
		$res=$this->model('model')->get_csvitems($excel);
		if(!$res[0]['编号'] && !$res[0]['任务类型名称']){
			$excel=cfc('tools')->importExcel($localexcel,true);
			$res=$this->model('model')->get_csvitems($excel);
			if(!$res[0]['编号'] && !$res[0]['任务类型名称']){
				cfc('files')->source_del($post['excel']);
				exi('导入文件异常，内容为空！','error');
			}
		}
		cfc('files')->source_del($post['excel']);
		$this->csv_add($res);
		exi('导入成功！');
	}
	public function csv_add($rows){
		foreach($rows as $item){
			if(!$item['编号']){
				//exi('有数据不存在姓名！','error');
			}
			$data=array(
				'sn'=>trim($item['编号']),
				'title'=>trim($item['任务类型名称']),
				'desc'=>trim($item['任务描述']),
				'age'=>trim($item['年龄']),
				'zhengshu'=>trim($item['资质证书']),
				'jineng'=>trim($item['专业技能']),
				'jinyan'=>trim($item['相关经验']),
				'tedian'=>trim($item['个性特点']),
			);
			if($item['性别']){
				$item['性别']=trim($item['性别']);
				$data['sex']=$item['性别']=='男'?1:2;
			}
			$xueli_type=$this->xueli_type;
			foreach($xueli_type as $k=>$xv){
				if(strpos($xv,$item['学历']) !== false){
					$data['xueli']=$k;
				}
			}
			$ptitle=trim($item['所属一级分类']);
			$pid=pdo_getcolumn('jy_aihire_job_group',array('uniacid'=>$_SESSION['uniacid'],'title'=>$ptitle),'id');
			if(!$pid){
				$pdata=array('title'=>$ptitle);
				$pid=$this->add($pdata);
			}
			$data['pid']=$pid;
			$isadd=pdo_get('jy_aihire_job_group',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'title'=>$data['title']),array('id','title'));
			if(!$isadd['id']){
				$this->add($data);
			}
		}
		return true;
	}
	public function add($postdata){
		$data=array(
			'title'=>trim($postdata['title']),
			'sn'=>$postdata['sn'],
			'desc'=>trim($postdata['desc']),
			'age'=>trim($postdata['age']),
			'zhengshu'=>trim($postdata['zhengshu']),
			'jineng'=>trim($postdata['jineng']),
			'jinyan'=>$postdata['jinyan'],
			'tedian'=>$postdata['tedian'],
			'sex'=>trim($postdata['sex']),
			'xueli'=>trim($postdata['xueli']),
			'pid'=>trim($postdata['pid']),
			'status'=>1,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_aihire_job_group',$data);
		$id=pdo_insertid();
		return $id;
	}
	public function get_csvitems($rows){
			$num = count($rows);
			$i = 0;
			$colsIndex = $rows[1];
			$rows = array_slice($rows, 1, count($rows) - 1);
			$items = array();
			$num = 0;
			foreach ($rows as $rownu => $rv) {
				$item = array();
				foreach ($colsIndex as $cols => $cv) {
						$item[$cv]=$rv[$cols];
				}
				$items[] = $item;
				++$num;
			}
			return $items;
	}
}
?>